package com.zyf.config;

import com.mysql.cj.util.StringUtils;
import com.zyf.entity.User;
import com.zyf.feign.UserFeign;
import java.io.Serializable;
import java.util.ArrayList;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 登录接口
 *
 * @author zhaoYuFei
 */
@Service("oauthUserService")
public class UserService implements UserDetailsService {
    @Resource
    UserFeign userFeign;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StringUtils.isNullOrEmpty(username)) {
            throw new UsernameNotFoundException("用户名为空");
        }
        //数据库查询真实数据
        User byUserName = userFeign.getByUserName(username);
        if (byUserName == null) {
            throw new UsernameNotFoundException("用户名或密码错误，请重新输入");
        }
        //查询权限

        // 初始化登录认证对象
        return new UserDetail(byUserName, new ArrayList<String>());
    }
}
